<?php

ini_set('display_errors', 1);
ini_set('memory_limit', -1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Doraemon\model\PointMonth as mPointMonth;
use Medoo\Medoo;


$startDate = '201805';
$endDate = '201903';
$userWhere = ['uid[<=]' => 1000000];
if (isset($argv[1])) {
    $num = $argv[1];
    if ($num == 1) {
        $userWhere = ['uid[>]' => 5000000, 'uid[<=]' => 5500000];
    } else if ($num == 2) {
        $userWhere = ['uid[>]' => 5500000, 'uid[<=]' => 6000000];
    } else if ($num == 3) {
        $userWhere = ['uid[>]' => 6000000];
    }
}

$tabStar = microtime(true);
$userList = [];
$page = 1;
$pageSize = 20000;
do {
    $limit = [($page - 1) * $pageSize, $pageSize];
    $userWhere['LIMIT'] = $limit;
    var_dump('开始查询member表' . $num . '#' . json_encode($limit) . '数据');
    $userList = \Doraemon\model\Member::select('uid', $userWhere);
    foreach ($userList as $uid) {
        $where = [
            'AND' => [
                'uid' => $uid,
                'month[>=]' => $startDate,
                'month[<=]' => $endDate
            ],
            'ORDER' => [
                'month' => 'ASC'
            ]
        ];
        $pointInfo = mPointMonth::getPointCountInfoByUid($uid);
        $point = max(0, $pointInfo['award_point_amount'] - $pointInfo['deduct_point_amount'] - $pointInfo['expiration_point_amount']);
        $currentPoint = mPointMonth::getCurrentPoint($uid);
        if ($point != $currentPoint) {
            var_dump('uid:' . $uid . '的可用积分不正确#current_point:' . $currentPoint . '#point:' . $point);
            $list = mPointMonth::select('*', $where);
            if ($list) {
                var_dump('开始执行uid:' . $uid);
                iniRemainPoint($list);
            }
        }
        if ($point < 0 || $currentPoint < 0) {
            var_dump('uid:' . $uid . '的可用积分小于零#current_point:' . $currentPoint . '#point:' . $point);
        }
    }
    $page++;
} while ($userList);


$tabEnd = microtime(true);
$time = $tabEnd - $tabStar;
var_dump('执行完毕。执行时间：' . number_format($time, 2) . 's ');

function iniRemainPoint($list)
{
    $updateMonthList = [];
    foreach ($list as $key => $row) {
        $debuctPoint = $row['deduct_point'];
        if ($debuctPoint > 0) {
            setRemainPoint($updateMonthList, $list, 0, $debuctPoint);
        } else {
            $updateMonthList[$row['id']]['point'] = $row['award_point'];
            $updateMonthList[$row['id']]['uid'] = $row['uid'];
        }
    }
    foreach ($updateMonthList as $id => $row) {
        $currentPoint = $row['point'];
        $uid = $row['uid'];
        mPointMonth::updateData(['remain_point' => $currentPoint], ['id' => $id, 'uid' => $uid]);
    }
}

function setRemainPoint(&$updateMonthList, &$list, $key, $point)
{
    $info = $list[$key];
    if (!isset($info['current_point'])) {
        $info['current_point'] = $info['award_point'];
        $updateMonthList[$list[$key]['id']]['point'] = $info['award_point'];
        $updateMonthList[$list[$key]['id']]['uid'] = $info['uid'];
    }
    if ($info['current_point']) {
        $remainPoint = $info['current_point'] - $point;
        if ($remainPoint < 0) {
            $updateMonthList[$list[$key]['id']]['point'] = 0;
            $list[$key]['current_point'] = 0;
            if (isset($list[$key + 1])) {
                setRemainPoint($updateMonthList, $list, $key + 1, abs($remainPoint));
            }
        } else {
            $updateMonthList[$list[$key]['id']]['point'] = $remainPoint;
            $list[$key]['current_point'] = $remainPoint;
            if (isset($list[$key + 1])) {
                setRemainPoint($updateMonthList, $list, $key + 1, 0);
            }
        }
    } else {
        if (isset($list[$key + 1])) {
            setRemainPoint($updateMonthList, $list, $key + 1, $point);
        }
    }
}


